<?php
namespace Admin\Controller;

class TeachplanController extends BaseController {


    public function index() {

        $map['teacher_id'] = $this->admin['id'];
        $map['term_id'] = $this->term['id'];
        $Teachplan = D ('Teachplan');
        $planList = $Teachplan->getTeachplanByWhere($map);
        $planList = $this->getCourseName($planList);


        $this->assign('planList',$planList);
        $this->display();
    }


    public function add() {
        if (!IS_POST) {

            // 获取当前老师的所有课程
            $Course = D ('Course');
            $courseList = $Course->getCourseByTid($this->admin['id']);
            if (empty($courseList)) $this->error('当前老师没有任何课程,请先添加课程');

            // 获取所有班级
            $map['department_id'] = $this->admin['department_id'];
            $classList = D ('Class')->getClassByWhere($map,'',true);

            $this->assign('classList',$classList);
            $this->assign('courseList',$courseList);
            $this->display();
        } else {

            $data['term_id'] = $this->term['id'];
            $data['teacher_id'] = $this->admin['id'];
            $data['name'] = I ('request.name','');
            $data['course_id'] = I ('request.course_id','');

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            // 获取课表相关数据
            $weeks = I ('request.week','');
            $xqs = I ('request.xq','');
            $starts = I ('request.start','');
            $ends = I ('request.end');
            $addresss = I ('request.address');
            $num = count($weeks);
            if (empty($weeks) || empty($xqs) || empty($starts) || empty($ends) || empty($addresss) || count($xqs)!=$num || count($starts)!= $num || count($ends)!=$num || count($addresss)!=$num)
                $this->out('RESULT_FAIL','课表数据错误');

            // 获取上课学生相关数据
            $Student = D ('Student');
            $class_ids = I ('request.class_id','');

            $newIdsArr = array();
            if (!empty($class_ids)) {
                foreach ($class_ids as $v) {
                    $newIdsArr = array_merge($Student->getSidsByCid($v),$newIdsArr);
                }
            }

            $student_ids = I ('request.student_id','');
            !empty($student_ids) && $newIdsArr = array_merge($newIdsArr,$student_ids);

            // 去除数组中得重复数据
            $newIdsArr = array_unique($newIdsArr);
            $data['student_ids'] = implode(',',$newIdsArr);
            if (empty($data['student_ids'])) $this->out('RESULT_FAIL','请添加上课学生');

            $Teachplan = D ('Teachplan');
            $Teachplan->startTrans();

            $flag = $Teachplan->_add($data);
            if ($flag === '000000') {
                $teachplan_id = $Teachplan->getLastInsID();
                $Timetable = D ('Timetable');

                for ($i=0;$i<$num;$i++) {
                    $ttData['week'] = $weeks[$i];
                    $ttData['xq'] = $xqs[$i];
                    $ttData['start'] = $starts[$i];
                    $ttData['end'] = $ends[$i];
                    $ttData['address'] = $addresss[$i];
                    $ttData['teachplan_id'] = $teachplan_id;

                    $flag = $Timetable->_add($ttData);
                    if ($flag !== '000000') {
                        $Teachplan->rollback();
                        $Timetable->delTimetableByTid($teachplan_id);
                        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
                    }
                }

                $Teachplan->commit();

                $this->out('RESULT_OK','',__CONTROLLER__);
            }
            $Teachplan->rollback();
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }
    }


    public function edit() {

        if (!IS_POST) {

            $id = I ('request.id');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Teachplan = D ('Teachplan');
            $t = $Teachplan->_get($id);
            if (empty($t)) $this->error('教学计划不存在');
            if ($t['teacher_id'] != $this->admin['id']) $this->error('非法访问');

            // 获取该教学计划中得所有课表信息
            $Timetable = D ('Timetable');
            $timetableList = $Timetable->getTimetableByTid($t['id']);
            //p($timetableList);

            // 获取所有参与该教学计划的学生
            $Student = D ('Student');
            $sidArr = explode(',',$t['student_ids']);
            $studentList = $this->getStudentClassName($Student->getStudentArrByIdArr($sidArr));

            // 获取当前老师的所有课程
            $Course = D ('Course');
            $courseList = $Course->getCourseByTid($this->admin['id']);
            if (empty($courseList)) $this->error('当前老师没有任何课程,请先添加课程');

            // 获取所有班级
            $map['department_id'] = $this->admin['department_id'];
            $classList = D ('Class')->getClassByWhere($map,'',true);

            $this->assign('classList',$classList);
            $this->assign('courseList',$courseList);

            $this->assign('teachplan',$t);
            $this->assign('timetableList',$timetableList);
            $this->assign('studentList',$studentList);
            $this->display();
        } else {

            $data['term_id'] = $this->term['id'];
            $data['teacher_id'] = $this->admin['id'];
            $data['id'] = I ('request.id','');
            $data['name'] = I ('request.name','');
            $data['course_id'] = I ('request.course_id','');

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Teachplan = D ('Teachplan');
            $t = $Teachplan->find($data['id']);
            if (empty($t) || $t['teacher_id'] !== $this->admin['id'])
                $this->out('RESULT_FAIL','非法操作');

            // 获取课表相关数据
            $weeks = I ('request.week','');
            $xqs = I ('request.xq','');
            $starts = I ('request.start','');
            $ends = I ('request.end');
            $addresss = I ('request.address');
            $num = count($weeks);
            if (empty($weeks) || empty($xqs) || empty($starts) || empty($ends) || empty($addresss) || count($xqs)!=$num || count($starts)!= $num || count($ends)!=$num || count($addresss)!=$num)
                $this->out('RESULT_FAIL','课表数据错误');

            // 获取上课学生相关数据
            $Student = D ('Student');
            $class_ids = I ('request.class_id','');

            $newIdsArr = array();
            if (!empty($class_ids)) {
                foreach ($class_ids as $v) {
                    $newIdsArr = array_merge($Student->getSidsByCid($v),$newIdsArr);
                }
            }

            $student_ids = I ('request.student_id','');
            !empty($student_ids) && $newIdsArr = array_merge($newIdsArr,$student_ids);

            // 去除数组中得重复数据
            $newIdsArr = array_unique($newIdsArr);
            $data['student_ids'] = implode(',',$newIdsArr);
            if (empty($data['student_ids'])) $this->out('RESULT_FAIL','请添加上课学生');

            $Teachplan->startTrans();

            $flag = $Teachplan->_save($data);
            if ($flag === '000000') {
                $teachplan_id = $data['id'];
                $Timetable = D ('Timetable');
                // 首先清空课表表
                $Timetable->delTimetableByTid($teachplan_id);

                for ($i=0;$i<$num;$i++) {
                    $ttData['week'] = $weeks[$i];
                    $ttData['xq'] = $xqs[$i];
                    $ttData['start'] = $starts[$i];
                    $ttData['end'] = $ends[$i];
                    $ttData['address'] = $addresss[$i];
                    $ttData['teachplan_id'] = $teachplan_id;

                    $flag = $Timetable->_add($ttData);
                    if ($flag !== '000000') {
                        $Teachplan->rollback();
                        $Timetable->delTimetableByTid($teachplan_id);
                        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
                    }
                }

                $Teachplan->commit();

                $this->out('RESULT_OK','',__CONTROLLER__);
            }
            $Teachplan->rollback();
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }

    }



    public function del() {
        if (!IS_POST) $this->error('非法访问');

        $id = I ('request.id','');
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Teachplan = D ('Teachplan');
        $t = $Teachplan->find($id);
        if (empty($t) || $t['teacher_id'] !== $this->admin['id'])
            $this->out('RESULT_FAIL','非法操作');

        $Teachplan->startTrans();
        if (($flag = $Teachplan->_del($id)) === '000000') {
            $Timetable = D ('Timetable');
            $f = $Timetable->delTimetableByTid($id);
            if ($f === '000000') {
                $Teachplan->commit();
                $this->out('RESULT_OK','',__CONTROLLER__);
            } else {
                $Teachplan->rollback();
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            }
        }
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }


    public function getStudent() {
        if (!IS_POST) $this->out('RESULT_FAIL','非法访问');

        $classid = I ('request.classid');
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($classid)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        //$map['class_id'] = $classid;
        //is_null($this->selectedSids) && $map['id'] = array('not in',$this->selectedSids);
        $studentList = D ('Student')->getStudentByCid($classid);
        $this->out('RESULT_OK','',$studentList);
    }


    /**
     * 获取所有学生的班级全名
     *
     * @param $data
     * @return mixed
     */
    protected function getStudentClassName($data) {
        $Class = D ('Class');

        $lid = $data[0]['class_id'];
        $nid = null;
        foreach ($data as $k=>$v) {
            $nid = $v['class_id'];
            if ($k != 0 && $nid === $lid) {
                $data[$k]['class_name'] = $data[$k-1]['class_name'];
                $lid = $nid;
                continue;
            }
            $data[$k]['class_name'] = $Class->getFullClassName($v['class_id']);
        }

        return $data;
    }


    protected function getCourseName ($data) {
        $Course = D ('Course');
        foreach ($data as $k=>$v) {
            $data[$k]['course_name'] = $Course->getCourseName($v['course_id']);
        }
        return $data;
    }

}